//
//  TFVoteCell.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/4/19.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFVoteCell: UITableViewCell {

    @objc public weak var delegate : TFVoteCellDelegate?
    @objc public var tf_width:CGFloat = 0
    @objc public var bgView = UIView()
    @objc public var nameLab = UILabel()
    @objc public var img = UIImageView()
    /**通知*/
    @objc public var model:TFBroadcast?
    {
        didSet{
            if(model != nil)
            {
                self.setModelUI(model: model!)
            }
        }
    }
    /**投票*/
    @objc public var votemodel:TFVoteData?
    {
        didSet{
            if(votemodel != nil)
            {
                self.setVotemodelUI(votemodel: votemodel!)
            }
        }
    }
    /**抽奖*/
    @objc public var LotteryStopmodel:TFLotteryStop?
    {
        didSet{
            if(LotteryStopmodel != nil)
            {
                self.setLotteryStopmodelUI(LotteryStopmodel: LotteryStopmodel!)
            }
        }
    }

    //yes 横屏; no 竖屏
    @objc public var isOrientationLandscape:Bool = false

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.bgView.backgroundColor = UIColor(red: 254/255.0, green: 232/255.0, blue: 227/255.0, alpha: 0.9)
        
        self.bgView.layer.cornerRadius = 14
        let tap = UITapGestureRecognizer.init(target: self, action:#selector(self.voteClick))

        self.bgView.addGestureRecognizer(tap)
        self.contentView.addSubview(self.bgView)
     

        self.nameLab.textColor = UIColor(red: 255/255.0, green: 115/255.0, blue: 87/255.0, alpha: 1)
       
        self.nameLab.font = UIFont.PingFang(size: 14)
  
        self.nameLab.numberOfLines = 0;
        self.nameLab.lineBreakMode = .byCharWrapping;
        self.bgView.addSubview(self.nameLab)
        

        self.img.image = UIImage.init(named: "HTLive.bundle/right_more")
        self.bgView.addSubview(self.img)

    }
    @objc func voteClick()
    {
        
        if self.delegate != nil && (self.delegate?.responds(to: #selector(TFVoteCellDelegate.clickVote(_:))))!{
            
            if (self.votemodel != nil) {
                self.delegate?.clickVote(self.votemodel!)
            }
            
        }

    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //通知
    func setModelUI(model:TFBroadcast)
    {
        self.img.isHidden = false
        
        var color = UIColor(red: 255/255.0, green: 115/255.0, blue: 87/255.0, alpha: 1)
        
        if (self.isOrientationLandscape) {
            color = UIColor.white
        } else {
            color = UIColor(red: 255/255.0, green: 115/255.0, blue: 87/255.0, alpha: 1)
        }
        let AttributedString = NSMutableAttributedString.getAttributed(string: model.msg , font: UIFont.PingFang(size: 14), foregroundColor: color)
        
         let size = TalkfunSharedTools.getSizeLabel(labelString: AttributedString, labelWidth:self.tf_width-24-50, labelfont: UIFont.PingFang(size: 14))
        
        
        if (self.isOrientationLandscape) {
            self.bgView.frame = CGRect(x: 12, y: 4, width: size.width+15+35, height: size.height+12)
         
            self.bgView.backgroundColor = UIColor(red: 255/255.0, green: 115/255.0, blue: 87/255.0, alpha: 0.9)
            self.img.image = UIImage.init(named: "HTLive.bundle/lingdang")

        } else {
            self.bgView.frame = CGRect(x: 12, y: 8, width: size.width+15+35, height: size.height+12)
            
            self.bgView.backgroundColor = UIColor(red: 254/255.0, green: 232/255.0, blue: 227/255.0, alpha: 0.9)
     
            self.img.image = UIImage.init(named: "HTLive.bundle/broadcast2")
        }
       
        self.img.frame = CGRect(x: 10, y: (self.bgView.frame.size.height-16)/2.0, width: 16, height: 16)
        self.nameLab.frame = CGRect(x: self.img.frame.maxX+9, y: 6, width: size.width, height: size.height) 
        self.nameLab.attributedText = AttributedString;
    }
    //投票
    func setVotemodelUI(votemodel:TFVoteData)
    {
        
        var color = UIColor(red: 255/255.0, green: 115/255.0, blue: 87/255.0, alpha: 1)


        self.img.isHidden = false
      
        if (self.isOrientationLandscape) {
            self.bgView.frame = CGRect(x: 12, y: 4, width: 225, height: 30)

            self.bgView.backgroundColor = UIColor(red: 255/255.0, green: 115/255.0, blue: 87/255.0, alpha: 0.9)
            
            self.img.image = UIImage.init(named: "HTLive.bundle/smallAcc")
            color = UIColor.white
        } else {
            self.bgView.frame = CGRect(x: 12, y: 8, width: 225, height: 30)

            self.bgView.backgroundColor = UIColor(red: 254/255.0, green: 232/255.0, blue: 227/255.0, alpha: 0.9)

            self.img.image = UIImage.init(named: "HTLive.bundle/smallAccRed")
            color = UIColor(red: 255/255.0, green: 115/255.0, blue: 87/255.0, alpha: 0.9)
        }
       
        self.bgView.isUserInteractionEnabled = true
        let str = "投票已结束，快去查看结果吧"
        let AttributedString = NSMutableAttributedString.getAttributed(string: str, font: UIFont.PingFang(size: 14), foregroundColor: color)

        self.nameLab.attributedText = AttributedString;
        self.nameLab.frame = CGRect(x: 16, y: 5, width: 190, height: 20)
      
        self.img.frame = CGRect(x: self.nameLab.frame.maxX, y: (self.bgView.frame.size.height-16)/2.0, width: 16, height: 16)
    }
    
    //抽奖
    func setLotteryStopmodelUI(LotteryStopmodel:TFLotteryStop)
    {
        self.img.isHidden = true
        var  color = UIColor(red: 255/255.0, green: 115/255.0, blue: 87/255.0, alpha: 1)
        if (self.isOrientationLandscape) {
            color = UIColor.white
        } else {
            color = UIColor(red: 255/255.0, green: 115/255.0, blue: 87/255.0, alpha: 1)
        }
        var str:String = "抽奖已结束,恭喜"

        let t = LotteryStopmodel.winnerList.count
        for i in 0..<t {
            let winner = LotteryStopmodel.winnerList[i] as! TFLotteryWinner
            if (i>3) {
                if (i==4) {
                    str = str + (winner.nickname as String) + "..." as String
               
                }
            } else {
                if (i==t-1) {
                    
                  str = str + winner.nickname
                 
                } else {
                   
                   str =  str + (winner.nickname as String) + "、"
                   
                }
            }
           
        }
        
        let allstr = str + (NSString.init(format: "等%ld人中奖", LotteryStopmodel.winnerList.count) as String)
        
        let  AttributedString = NSMutableAttributedString.getAttributed(string: allstr , font: UIFont.PingFang(size: 14), foregroundColor: color)
        
        
         let size = TalkfunSharedTools.getSizeLabel(labelString: AttributedString, labelWidth:self.tf_width-28-32, labelfont: UIFont.PingFang(size: 14))
        
        
        if (self.isOrientationLandscape) {
            self.bgView.frame = CGRect(x: 12, y: 4, width: size.width+32, height: size.height+14)
            self.bgView.backgroundColor = UIColor(red: 255/255.0, green: 115/255.0, blue: 87/255.0, alpha: 0.9)
        } else {
            self.bgView.frame = CGRect(x: 12, y: 8, width: size.width+32, height: size.height+14)
            self.bgView.backgroundColor = UIColor(red: 254/255.0, green: 232/255.0, blue: 227/255.0, alpha: 0.9)
           
        }
       

        self.nameLab.frame = CGRect(x: 16, y: 6, width: size.width, height: size.height)
      
        self.nameLab.attributedText = AttributedString;
    }
    
}

@objc protocol TFVoteCellDelegate : NSObjectProtocol {
    //全屏图片
    func clickVote(_ model : TFVoteData)

}
